/**
  ******************************************************************************
  * @file    ct75.c
  * @author  fanhui.meng
  * @version V1.0.0
  * @date    2019.9.20
  * @brief   
  ******************************************************************************
  */

#include "i2c.h"
#include "stdio.h"
#include "i2c_app.h"

/**************************pca9545**************************************/
void test_select_pca_channel(void)
{
	uint8_t channel_value_write = CHANNEL_0_MP5416;
	uint8_t channel_value_read = 0;
	
	if (HAL_I2C_Mem_Write(&hi2c2, (PCA9545_ADDR<<1)|0x00, 0x00, I2C_MEMADD_SIZE_8BIT,&channel_value_write,1, 1000) != HAL_OK)
	{
	  printf("\r\n Select_pca_channel write failed\n");
	}
	if (HAL_I2C_Mem_Read(&hi2c2, (PCA9545_ADDR<<1)|0x01, 0x00, I2C_MEMADD_SIZE_8BIT,&channel_value_read,1, 1000) != HAL_OK)
	{
	  printf("\r\n Select_pca_channel 2 failed\n");
	} else {
	  printf("\r\n Select_pca_channel %d \n",channel_value_read);
	}
	
}
void select_pca_channel(uint8_t channel)
{
	uint8_t channel_value = channel;
	
	/*if (HAL_I2C_Master_Transmit(&hi2c2, (PCA9545_ADDR<<1)|0x00, &channel_value, 1, 1000) != HAL_OK)
	{
	  printf("\r\n Select_pca_channel failed\n");
	}*/
	if (HAL_I2C_Mem_Write(&hi2c2, (PCA9545_ADDR<<1)|0x00, 0x00, I2C_MEMADD_SIZE_8BIT,&channel_value,1, 1000) != HAL_OK)
	{
	  printf("\r\n Select_pca_channel failed\n");
	}

}


/****************************************CT75*******************************************************************************/
void CT75_Alert_INIT(void)
{
	CT75_SET_ALERT_LIMIT(CT75_WRITE_ADDR1);
	CT75_SET_ALERT_LIMIT(CT75_WRITE_ADDR2);
	CT75_SET_ALERT_LIMIT(CT75_WRITE_ADDR3);
}

int16_t CT75_Read_Temperature(uint16_t ct75_addr)
{
	uint8_t CT75_Temperature_Data_Buffer[CT75_TEMPERATURE_NUMBER];
	int16_t ut;
    

	if(HAL_I2C_Mem_Read(&hi2c1, ct75_addr, CT75_CMD_ADDR, I2C_MEMADD_SIZE_8BIT,CT75_Temperature_Data_Buffer,CT75_TEMPERATURE_NUMBER, 1000) != HAL_OK)
	{
		CT75_TIMEOUT_UserCallback();
	}

	ut = (int16_t)(((((uint16_t)CT75_Temperature_Data_Buffer[0]) << 8) | (uint16_t)CT75_Temperature_Data_Buffer[1]) >> 4); //ct75  >> 4 lm75 >>5
	return ut;
}

void CT75_TIMEOUT_UserCallback(void)
{
	printf("I2C_Read_NBytes failed\r\n");
}

void CT75_SET_ALERT_LIMIT(uint16_t ct75_addr)
{
	uint8_t reg_val[CT75_TEMPERATURE_NUMBER];
	uint8_t  reg_addr;
 
	reg_addr = 0x03;
	reg_val[0]  = 0x50; //80 degree
	reg_val[1]  = 0x00;
	if(HAL_I2C_Mem_Write(&hi2c2, ct75_addr, reg_addr, I2C_MEMADD_SIZE_8BIT,reg_val,CT75_TEMPERATURE_NUMBER, 1000) != HAL_OK)
	{
		CT75_TIMEOUT_UserCallback();
	}
	//Set the TL limit
	reg_addr = 0x02;
	reg_val[0]  = 0x4B; //75 degree 
	reg_val[1]  = 0x00;
	if(HAL_I2C_Mem_Write(&hi2c2, ct75_addr, reg_addr, I2C_MEMADD_SIZE_8BIT,reg_val,CT75_TEMPERATURE_NUMBER, 1000) != HAL_OK)
	{
		CT75_TIMEOUT_UserCallback();
	}
}

int test_ct75(void)
{
	float TempReadValue[3] = {0};
	float Temperature1;

	TempReadValue[0] = (float)CT75_Read_Temperature(CT75_WRITE_ADDR1) * 0.0625;//0.125;
	Temperature1 = (uint16_t)(TempReadValue[0] * 100);
	printf("Temperature1:%f\r\n", (float)Temperature1 / 100);
	return 0;
}

/****************************************eeprom*******************************************************************************/
int test_eeprom(void)
{
	uint8_t I2C_Buffer_Write[256];
	uint8_t I2C_Buffer_Read[256];
	uint16_t i = 0;
	printf("\r\n \r\n*********STM32CubeMX I2C AT24C02 Example*********\r\n");
	printf("\r\n I2C Write Buffer:\r\n");
	for(i=0; i<256; i++)
	{
		I2C_Buffer_Write[i]=i;    /* WriteBuffer Initialization */
		printf("%02X ", I2C_Buffer_Write[i]);

	}
	for(i=0; i<256; i=i+8)
	{
		if (HAL_I2C_Mem_Write(&hi2c1, ADDR_AT24C02_Write, i, I2C_MEMADD_SIZE_8BIT,&(I2C_Buffer_Write[i]),8, 1000) == HAL_OK)
		{
		  printf("\r\n Byte %02d to Byte %02d Write OK",i,i+8);
		  HAL_Delay(5);//写完以后需要延时5ms，这个不能少
		}
		else
		  printf("\r\n Byte %02d to Byte %02d Write Failed",i,i+8);
	}
	HAL_I2C_Mem_Read(&hi2c1, ADDR_AT24C02_Read, 0, I2C_MEMADD_SIZE_8BIT,I2C_Buffer_Read,256, 1000);
	for(i=0; i<256; i++)
	{
		printf("%02X  ",I2C_Buffer_Read[i]);
	}
	return 0;
}

/****************************************主从模式测试******************************************************************************/
/*验证从中断接收*/
uint8_t I2C_slave_send[2] = {4};
uint8_t I2C_send[1] = {1};
uint8_t I2C_Buffer_Slave[TEST_NUM] = {0};
uint8_t I2C_Buffer_master_rsv[TEST_NUM] = {0};
void test_slave_mode(void)
{
	uint8_t i2c_slave_add = I2C2_SLAVE_ADD;
	//printf("\r\n \r\n*********I2C slave Example*********\r\n");
	HAL_I2C_Slave_Receive_IT(&hi2c1/*接收的I2C*/,I2C_Buffer_Slave/*存放数据的地址*/,sizeof(I2C_Buffer_Slave)/*接收数据的个数*/);
	while(1)
	{
		if (HAL_I2C_Mem_Write(&hi2c2, (i2c_slave_add<<1)|0x00, 0x00,I2C_MEMADD_SIZE_8BIT,I2C_send,TEST_NUM, 1000) != HAL_OK)
		{
		  printf("\r\n Byte  Write 1 Failed\n");
		}
		
		HAL_Delay(1000);
	}
	
}

void test_slave_mode_print(void)
{
	int i;
	for(i=0; i<TEST_NUM; i++)
	{
		printf("%d  ",I2C_Buffer_Slave[i]);
		I2C_Buffer_Slave[i] = 0;
	}
	printf("\n");

}

/* 从机接收回调函数 
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
	test_slave_mode_print();

	HAL_I2C_Slave_Receive_IT(&hi2c1,I2C_Buffer_Slave,sizeof(I2C_Buffer_Slave));

}*/

void test_i2c_write(void)
{
	uint8_t send_data = 0x45;
	uint8_t i2c_slave_add = I2C2_SLAVE_ADD;
	
	HAL_I2C_Mem_Write(&hi2c1, (i2c_slave_add<<1)|0x00, 0x00, I2C_MEMADD_SIZE_8BIT, &send_data, 1, 1000);
}

/*验证i2c主读，从发*/
uint8_t mas_rcv_data = 0;
uint8_t slave_rcv_data = 0;
uint8_t mas_send_data = 0x55;
uint8_t i2c_slave_add = I2C1_SLAVE_ADD;
void test_i2c_read(void)
{
	uint8_t Temp[256] = {0x00,0x01,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69};
	uint8_t i = 0;
	test_bmc_func();
	//HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &slave_rcv_data, 1, I2C_FIRST_AND_NEXT_FRAME);
	while(1)
	{
		if (i>9)i = 0;
		HAL_I2C_Mem_Read(&hi2c2, (i2c_slave_add<<1)|0x01, Temp[i], I2C_MEMADD_SIZE_8BIT, &mas_rcv_data, 1, 1000);//polling mode
		i++;
		printf("r:%x\n",mas_rcv_data);
		HAL_Delay(1000);
	}
	
}

/*********************从中断读响应**********************************/
void test_bmc_func(void)
{
	  HAL_I2C_EnableListen_IT(&hi2c1);
}

uint8_t reg_vale;
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
{
	if(hi2c == &hi2c1)
		{
		   HAL_I2C_EnableListen_IT(&hi2c1);
		   HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &slave_rcv_data, 1, I2C_FIRST_AND_NEXT_FRAME);
		}
}

void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
	uint8_t Direction = TransferDirection;
	uint8_t Temp[256] = {0x40,0x01,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69};
 
	__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
 
 
	if(hi2c == &hi2c1)
	{
	  if(Direction == 1)//Read 5320
	  {
		 HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, &Temp[reg_vale], 2, I2C_FIRST_FRAME);
	  }	else{
		HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &slave_rcv_data, 1, I2C_FIRST_AND_NEXT_FRAME);
	  }

	} 
	//HAL_I2C_EnableListen_IT(&hi2c1);
}

void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
	printf("reg:%x ",slave_rcv_data);
	reg_vale = slave_rcv_data;
	if (slave_rcv_data>0x61)
	{
		reg_vale = slave_rcv_data - 0x62 + 2;
	}
	//HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &slave_rcv_data, 1, I2C_FIRST_AND_NEXT_FRAME);

}
